<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores'
import { message } from '@/components/ZyMessage/index.js'
import { storeToRefs } from 'pinia'
import { avatarColor } from '@/assets/data.js'
import { updateUserAPI } from '@/api/user.js'
import { uploadAPI } from '@/api/common.js'
import ImgCutter from 'vue-img-cutter'

const router = useRouter()
const userStore = useUserStore()
const { user } = storeToRefs(userStore)

// 修改
const loading = ref(false)
const imgFile = ref(null)
const params = reactive({ nickname: user.value.nickname, avatar: user.value.avatar })
const onSubmit = async () => {
  loading.value = true
  try {
    // 头像是否修改过
    if (user.value.avatar !== params.avatar) {
      // 上传头像
      const formdata = new FormData()
      formdata.append('file', imgFile.value)
      params.avatar = await uploadAPI(formdata)
    }
    await updateUserAPI(params)
    userStore.updateUser(params)
    message('修改成功')
    router.push('/')
  } finally {
    loading.value = false
  }
}

// 裁剪图片
const onCutDownImg = (res) => {
  // 1.获取文件
  const file = res.file
  imgFile.value = file
  // 2.限制文件大小
  const maxSize = 1024 * 1024 * 5 // 5MB
  if (file.size > maxSize) return message('图片不能大于5MB', 'warning')
  // 3.创建FileReader
  const reader = new FileReader()
  reader.onload = (e) => {
    params.avatar = e.target.result
  }
  reader.readAsDataURL(file)
}
</script>

<template>
  <div class="account-container">
    <!-- 顶部 -->
    <div class="head">
      <img class="logo" src="@/assets/images/logo.svg" alt="" />
      <span class="title">知语拾光</span>
    </div>
    <!-- main -->
    <div class="main">
      <p class="title">账号信息</p>
      <div class="item">
        <div class="label">{{ user.username ? '用户名' : '游客' }}</div>
        <input class="input disabled" :value="user.username || user.ip" type="text" readonly />
      </div>
      <div class="item">
        <div class="label">昵称</div>
        <input class="input" v-model="params.nickname" type="text" placeholder="昵称" />
      </div>
      <div class="item">
        <div class="label">头像</div>
        <div class="avatar">
          <!-- 判断头像是否有效 -->
          <img
            class="avatar"
            :src="params.avatar"
            v-if="params.avatar?.startsWith('http') || params.avatar?.startsWith('data:')"
          />
          <div class="avatar" v-else :style="{ backgroundImage: avatarColor[params.avatar] }"></div>
          <!-- 头像裁剪 -->
          <img-cutter
            ref="imgCutterModal"
            @cutDown="onCutDownImg"
            :boxWidth="700"
            :boxHeight="458"
            :cutWidth="240"
            :cutHeight="240"
          >
            <template #open>
              <div class="mask">
                <svg-icon class="change-icon" icon-name="qiehuan" />
              </div>
            </template>
          </img-cutter>
        </div>
      </div>
      <!-- 提交 -->
      <zy-button class="submit" :loading="loading" @click="onSubmit">修改</zy-button>
      <!-- 返回首页 -->
      <div class="bottom">
        <p class="return" @click="$router.push('/')">返回首页</p>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.account-container {
  width: 100%;
  min-height: 100vh;
  background-color: #f4f5f7;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.head {
  display: flex;
  align-items: center;
  .logo {
    width: 45px;
  }
  .title {
    font-size: 32px;
    color: #202020;
    font-weight: 600;
    margin-left: 8px;
  }
}
.main {
  width: 400px;
  background-color: #ffffff;
  border-radius: 8px;
  padding: 40px;
  margin-top: 30px;
  .title {
    font-size: 20px;
    font-weight: 600;
    line-height: 28px;
    margin-bottom: 30px;
  }
  .item {
    margin-bottom: 20px;
    .label {
      margin-bottom: 8px;
    }
    .input {
      height: 48px;
      width: 100%;
      padding: 0 16px;
      border: none;
      border-radius: 8px;
      background-color: #f6f6f8;
      outline: none;
    }
  }
  .submit {
    margin-top: 10px;
    width: 100%;
    background-color: #202020;
    height: 48px;
    font-size: 16px;
    border-radius: 8px;
  }
  .bottom {
    margin-top: 12px;
    display: flex;
    justify-content: space-between;
    p {
      color: #1890ff;
      cursor: pointer;
    }
    .return {
      color: #949494;
    }
  }
}
.avatar {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  .mask {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: all 0.3s;
    .change-icon {
      font-size: 22px;
      color: #fff;
    }
  }
  &:hover .mask {
    opacity: 1;
  }
}
.disabled {
  cursor: not-allowed;
  color: #949494;
}
</style>
